package com.abss.vote.util;

import com.abss.vote.model.MpInfo;
import com.abss.vote.service.MpInFoService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 工具类集
 *
 */
public class Utils {

    //获取本服务器上边的配置文件信息
//    public static MpInfo getLocalMpInFo(){
//        MpInfo mpInfo = new MpInfo();
//        try {
//            List<String> ls= FileUtils.getBanner();//获取配置文件中的数据
//            mpInfo.setSERVER_ID(ls.get(0));
//            mpInfo.setSERVER_NAME(ls.get(1));
//            mpInfo.setMP_APP_ID(ls.get(2));
//            mpInfo.setMP_APP_SECRET(ls.get(3));
//            mpInfo.setWXCALLBACK(ls.get(4));
//            mpInfo.setDOMAIN_NAME(ls.get(5));
//            mpInfo.setFILE_PATH(ls.get(6));
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return mpInfo;
//    }

    @Autowired
    MpInFoService mpInFoService;

    public MpInfo getMpInFo(String serverId){
        MpInfo mpInfo = mpInFoService.getMpInFoByServerId(serverId);
        return mpInfo;
    }

    public static MpInfo getFilePath(String serverId){
        Utils utils = new Utils();
        MpInfo mpInfo =utils.getMpInFo(serverId);
        return mpInfo;
    }

    /**
     * 手机号随机验证码
     * @return
     */
    public static int getRandom() {
        return ((int) ((Math.random() * 9 + 1) * 100000));
    }

    /**
     * 得到1970年01月1日0点零分以来的毫秒数
     * 用作图片、音频、视频的文件名
     * @return
     */
    public static long getName() {
        return new Date().getTime();
    }

    /**
     * 获取今天凌晨
     * @param date
     * @return
     */
    public static Date setTimeEnd(Date date){
        if (date == null)
            return null;
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 00);
        c.set(Calendar.MINUTE, 00);
        c.set(Calendar.SECOND, 00);
        return c.getTime();
    }

    /**
     * 将获取的最后时间格式化
     * @return
     */
    public static Date getDate(){
        Date date = Utils.setTimeEnd(new Date());
        return date;
    }

    /**
     * 使用UUID第28位到最后
     * @return
     */
    public static String getAddressID() {
        String orNumber = UUID.randomUUID().toString();
        return orNumber.substring(27);
    }

    /**
     * 获取今天凌晨
     * @param date
     * @return
     */
    public static Date setTwoBeforTimeEnd(Date date){
        if (date == null)
            return null;
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.SECOND, -6000);
        return c.getTime();
    }

    /**
     * 将获取的最后时间格式化
     * @return
     */
    public static Date gettwoDate(){
        Date date = Utils.setTwoBeforTimeEnd(new Date());
        return date;
    }



    /**
     * 获取前台资源
     * @param urlString url链接 不带域名
     * @return 获取到的字符串资源
     */
    public static String getHtml(String urlString){
        try {
            URL url = new URL(urlString);
            // 打开URL
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            // 得到输入流，即获得了网页的内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));

            String line;
            // 读取输入流的数据，并显示
            String content="";          //翻译结果
            while ((line = reader.readLine()) != null) {            //读取内容
                content+=line;
            }
            return content;//返回结果

        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

}
